package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.StoreInfo;
import com.ruoyi.business.mapper.StoreInfoMapper;
import com.ruoyi.business.service.IStoreInfoService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 门店信息Service业务层处理
 *
 * @author AC
 * @date 2022-11-24
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements IStoreInfoService {

    @Autowired
    private ISysUserService userService;

    /**
     * 查询门店信息列表
     *
     * @param storeInfo 门店信息
     * @return 门店信息
     */
    @Override
    public List<StoreInfo> selectStoreInfoList(StoreInfo storeInfo) {
        return getBaseMapper().selectStoreInfoList(storeInfo);
    }

    @Override
    public boolean save(StoreInfo entity) {
        entity.setCreatedTime(new Date());
        entity.setCreatorId(SecurityUtils.getUserId());
        SysUser sysUser = userService.selectUserById(entity.getManagerId());
        entity.setManagerName(sysUser.getNickName());
        entity.setManagerTel(sysUser.getPhonenumber());
        return super.save(entity);
    }


    @Override
    public boolean updateById(StoreInfo entity) {
        entity.setUpdatedTime(new Date());
        entity.setUpdatorId(SecurityUtils.getUserId());
        if (entity.getStatus() != null && entity.getStatus().equals(2)) {
            throw new ServiceException("更新时不允许更新状态为删除");
        }
        SysUser sysUser = userService.selectUserById(entity.getManagerId());
        entity.setManagerName(sysUser.getNickName());//管理员修改后存进去
        entity.setManagerTel(sysUser.getPhonenumber());//电话号码修改加进去
        return super.updateById(entity);
    }

    @Override
    public boolean removeBatchByIds(Collection<?> list) {
        UpdateWrapper<StoreInfo> wrapper =
                new UpdateWrapper<StoreInfo>()
                        .set("status", 2)
                        .in("id", list);

        return super.update(wrapper);
    }
}
